home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / sound / players / maplay_t.z / maplay_t / subband_layer_1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  8.4 KB  |  273 lines

  1. /*
  2.  *  @(#) subband_layer_1.c 1.5, last edit: 2/21/94 18:10:02
  3.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  4.  *  @(#) Berlin University of Technology
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include "subband_layer_1.h"
  23. #include "scalefactors.h"
  24.  
  25.  
  26. // factors and offsets for sample requantization:
  27. static const real table_factor[15] = {
  28.   0.0, (1.0/2.0) * (4.0/3.0), (1.0/4.0) * (8.0/7.0), (1.0/8.0) * (16.0/15.0),
  29.   (1.0/16.0) * (32.0/31.0), (1.0/32.0) * (64.0/63.0), (1.0/64.0) * (128.0/127.0),
  30.   (1.0/128.0) * (256.0/255.0), (1.0/256.0) * (512.0/511.0),
  31.   (1.0/512.0) * (1024.0/1023.0), (1.0/1024.0) * (2048.0/2047.0),
  32.   (1.0/2048.0) * (4096.0/4095.0), (1.0/4096.0) * (8192.0/8191.0),
  33.   (1.0/8192.0) * (16384.0/16383.0), (1.0/16384.0) * (32768.0/32767.0)
  34. };
  35.  
  36. static const real table_offset[15] = {
  37.   0.0, ((1.0/2.0)-1.0) * (4.0/3.0), ((1.0/4.0)-1.0) * (8.0/7.0), ((1.0/8.0)-1.0) * (16.0/15.0),
  38.   ((1.0/16.0)-1.0) * (32.0/31.0), ((1.0/32.0)-1.0) * (64.0/63.0), ((1.0/64.0)-1.0) * (128.0/127.0),
  39.   ((1.0/128.0)-1.0) * (256.0/255.0), ((1.0/256.0)-1.0) * (512.0/511.0),
  40.   ((1.0/512.0)-1.0) * (1024.0/1023.0), ((1.0/1024.0)-1.0) * (2048.0/2047.0),
  41.   ((1.0/2048.0)-1.0) * (4096.0/4095.0), ((1.0/4096.0)-1.0) * (8192.0/8191.0),
  42.   ((1.0/8192.0)-1.0) * (16384.0/16383.0), ((1.0/16384.0)-1.0) * (32768.0/32767.0)
  43. };
  44.  
  45.  
  46.  
  47. /**********************/    // used for single channel mode
  48. /*** Standard Class ***/    // and in derived class for intensity
  49. /**********************/    // stereo mode
  50.  
  51. SubbandLayer1::SubbandLayer1 (uint32 subbandnumber)
  52. {
  53.   this->subbandnumber = subbandnumber;
  54.   samplenumber = 0;
  55. }
  56.  
  57.  
  58. void SubbandLayer1::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  59. {
  60.   if ((allocation = stream->get_bits (4)) == 15)
  61.     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  62.   if (crc)
  63.     crc->add_bits (allocation, 4);
  64.   if (allocation)
  65.   {
  66.     samplelength = allocation + 1;
  67.     factor = table_factor[allocation];
  68.     offset = table_offset[allocation];
  69.   }
  70. }
  71.  
  72.  
  73. void SubbandLayer1::read_scalefactor (Ibitstream *stream, Header *)
  74. {
  75.   if (allocation)
  76.     if ((scalefactor = stream->get_bits (6)) == 63)
  77.       cerr << "WARNING: stream contains an illegal scalefactor!\n";    // MPEG-stream is corrupted!
  78. }
  79.  
  80.  
  81. bool SubbandLayer1::read_sampledata (Ibitstream *stream)
  82. {
  83.   if (allocation)
  84.   {
  85.     sample = real (stream->get_bits (samplelength));
  86. #ifdef DEBUG
  87.     if (sample == (1 << samplelength) - 1)
  88.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  89. #endif
  90.   }
  91.   if (++samplenumber == 12)
  92.   {
  93.     samplenumber = 0;
  94.     return True;
  95.   }
  96.   else
  97.     return False;
  98. }
  99.  
  100.  
  101. bool SubbandLayer1::put_next_sample (e_channels channels,
  102.                      SynthesisFilter *filter1, SynthesisFilter *filter2)
  103. {
  104.   if (allocation && channels != right)
  105.   {
  106.     register real scaled_sample = (sample * factor + offset) * scalefactors[scalefactor];
  107. #ifdef DEBUG
  108.     if (scaled_sample < -1.0 || scaled_sample > 1.0)
  109.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  110.       // this should never occur
  111. #endif
  112.     if (scaled_sample < -1.0E-7 || scaled_sample > 1.0E-7)
  113.       filter1->input_sample (scaled_sample, subbandnumber);
  114.   }
  115.   return True;
  116. }
  117.  
  118.  
  119. /******************************/
  120. /*** Intensity Stereo Class ***/
  121. /******************************/
  122.  
  123. SubbandLayer1IntensityStereo::SubbandLayer1IntensityStereo (uint32 subbandnumber)
  124. : SubbandLayer1 (subbandnumber)
  125. {
  126. }
  127.  
  128.  
  129. void SubbandLayer1IntensityStereo::read_scalefactor (Ibitstream *stream, Header *)
  130. {
  131.   if (allocation)
  132.   {
  133.     scalefactor = stream->get_bits (6);
  134.     channel2_scalefactor = stream->get_bits (6);
  135.     if (scalefactor == 63 || channel2_scalefactor == 63)
  136.       cerr << "WARNING: stream contains an illegal scalefactor!\n";
  137.       // MPEG-stream is corrupted!
  138.   }
  139. }
  140.  
  141.  
  142. bool SubbandLayer1IntensityStereo::put_next_sample (e_channels channels,
  143.     SynthesisFilter *filter1, SynthesisFilter *filter2)
  144. {
  145.   if (allocation)
  146.   {
  147.     sample = sample * factor + offset;        // requantization
  148.     if (channels == both)
  149.     {
  150.       register real sample1 = sample * scalefactors[scalefactor],
  151.             sample2 = sample * scalefactors[channel2_scalefactor];
  152. #ifdef DEBUG
  153.       if (sample1 < -1.0 || sample1 > 1.0 || sample2 < -1.0 || sample2 > 1.0)
  154.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  155.     // this should never occur
  156. #endif
  157.       if (sample1 < -1.0E-7 || sample1 > 1.0E-7)
  158.     filter1->input_sample (sample1, subbandnumber);
  159.       if (sample2 < -1.0E-7 || sample2 > 1.0E-7)
  160.     filter2->input_sample (sample2, subbandnumber);
  161.     }
  162.     else if (channels == left)
  163.     {
  164.       register real sample1 = sample * scalefactors[scalefactor];
  165. #ifdef DEBUG
  166.       if (sample1 < -1.0 || sample1 > 1.0)
  167.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  168.     // this should never occur
  169. #endif
  170.       if (sample1 < -1.0E-7 || sample1 > 1.0E-7)
  171.     filter1->input_sample (sample1, subbandnumber);
  172.     }
  173.     else
  174.     {
  175.       register real sample2 = sample * scalefactors[channel2_scalefactor];
  176. #ifdef DEBUG
  177.       if (sample2 < -1.0 || sample2 > 1.0)
  178.     cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  179.     // this should never occur
  180. #endif
  181.       if (sample2 < -1.0E-7 || sample2 > 1.0E-7)
  182.     filter1->input_sample (sample2, subbandnumber);
  183.     }
  184.   }
  185.   return True;
  186. }
  187.  
  188.  
  189.  
  190. /********************/
  191. /*** Stereo Class ***/
  192. /********************/
  193.  
  194. SubbandLayer1Stereo::SubbandLayer1Stereo (uint32 subbandnumber)
  195. : SubbandLayer1 (subbandnumber)
  196. {
  197. }
  198.  
  199.  
  200. void SubbandLayer1Stereo::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
  201. {
  202.   allocation = stream->get_bits (4);
  203.   channel2_allocation = stream->get_bits (4);
  204.   if (crc)
  205.   {
  206.     crc->add_bits (allocation, 4);
  207.     crc->add_bits (channel2_allocation, 4);
  208.   }
  209.   if (allocation == 15 || channel2_allocation == 15)
  210.     cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  211.   if (allocation)
  212.   {
  213.     samplelength = allocation + 1;
  214.     factor = table_factor[allocation];
  215.     offset = table_offset[allocation];
  216.   }
  217.   if (channel2_allocation)
  218.   {
  219.     channel2_samplelength = channel2_allocation + 1;
  220.     channel2_factor = table_factor[channel2_allocation];
  221.     channel2_offset = table_offset[channel2_allocation];
  222.   }
  223. }
  224.  
  225.  
  226. void SubbandLayer1Stereo::read_scalefactor (Ibitstream *stream, Header *)
  227. {
  228.   if (allocation)
  229.     if ((scalefactor = stream->get_bits (6)) == 63)
  230.       cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  231.   if (channel2_allocation)
  232.     if ((channel2_scalefactor = stream->get_bits (6)) == 63)
  233.       cerr << "WARNING: stream contains an illegal allocation!\n";    // MPEG-stream is corrupted!
  234. }
  235.  
  236.  
  237. bool SubbandLayer1Stereo::read_sampledata (Ibitstream *stream)
  238. {
  239.   bool returnvalue = SubbandLayer1::read_sampledata (stream);
  240.   if (channel2_allocation)
  241.   {
  242.     channel2_sample = real (stream->get_bits (channel2_samplelength));
  243. #ifdef DEBUG
  244.     if (channel2_sample == (1 << channel2_samplelength) - 1)
  245.     cerr << "WARNING: stream contains an illegal subband sample!\n";  // MPEG-stream is corrupted!
  246. #endif
  247.   }
  248.   return returnvalue;
  249. }
  250.  
  251.  
  252. bool SubbandLayer1Stereo::put_next_sample (e_channels channels,
  253.                        SynthesisFilter *filter1, SynthesisFilter *filter2)
  254. {
  255.   SubbandLayer1::put_next_sample (channels, filter1, filter2);
  256.   if (channel2_allocation && channels != left)
  257.   {
  258.     register float sample2 = (channel2_sample * channel2_factor + channel2_offset)
  259.                  * scalefactors[channel2_scalefactor];
  260. #ifdef DEBUG
  261.     if (sample2 < -1.0 || sample2 > 1.0)
  262.       cerr << "WARNING: rescaled subband sample is not in [-1.0, 1.0]\n";
  263.       // this should never occur
  264. #endif
  265.     if (sample2 < -1.0E-7 || sample2 > 1.0E-7)
  266.       if (channels == both)
  267.     filter2->input_sample (sample2, subbandnumber);
  268.       else
  269.     filter1->input_sample (sample2, subbandnumber);
  270.   }
  271.   return True;
  272. }
  273.